/**
 * Created by Administrator on 2017/3/3.
 */

var dao = require('../dao/LoginDao');


module.exports = {
    userLogin:function (openid,phone,callback) {
        dao.userLogin(openid,phone,function (data) {
            callback(data)
        })
    },
    selectByPhone: function (phone, callback) {
        var users = [];
        dao.selectByPhone(phone, function (data) {
            if (data.isSuccess) {
                users = data.rows;
                callback(users);
            } else {
                callback();
            }
        })
    },
    selectSMSCodeByPhone: function (phone, callback) {
        var users = [];
        dao.selectByPhone(phone, function (data) {
            if (data.isSuccess) {
                if (data.rows.length > 0) {
                    data.rows.forEach(function (val) {
                        users.push({
                            id: val.id,
                            phone: val.phone,
                            createtime: val.createtime,
                            expiretime: val.expiretime,
                            username: val.username,
                            isdel: val.isdel,
                            smscode: val.smscode,
                            times: val.times
                        });
                    });
                }
                callback(users);
            } else {
                callback();
            }
        })
    },

    createSmsCode: function (smscode, phone, callback) {
        console.log("usermodel");
        var users = [];
        dao.selectByPhone(phone, function (data) {
            if (!data) {
                dao.addUser(phone, function (add) {
                    if (add.isSuccess == true) {
                        dao.createSmsCode(smscode, phone, function (err, add) {
                            if (add.isSuccess) {
                                callback(add.rows);
                            } else {
                                callback();
                            }
                        })
                    }
                })
            } else {
                dao.createSmsCode(smscode, phone, function (data) {
                    if (data.isSuccess) {
                        callback(data);
                    } else {
                        callback();
                    }
                })
            }
        });
    },

    changePhone: function (_params, callback) {
        dao.changePhone(_params, function (data) {
            callback(data)
        })
    },
    getCardIdByUserId:function (_params, callback) {

        dao.getCardIdByUserId(_params,function (result) {
            callback(result)
        })
    },
    changeUser:function (_params, callback) {
        dao.changeUser(_params,function (result) {
            callback(result);
        })

    },
    addAndLogin: function (openid, phone, userInfo, callBack) {
        console.log('addandlogindao'+[openid, phone, userInfo])
        dao.addAndLogin(openid, phone, userInfo,function (reslut) {
            callBack(reslut);
        })
    }



}

